﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;

namespace BYManageInfoSystem
{
    public partial class Frm_FactoryAddr : DockContent
    {
        private DGBY.BLL.FacAddrTab m_bllFacAddrTab = new DGBY.BLL.FacAddrTab();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectFacAddrTabID = 0;
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];

        public Frm_FactoryAddr()
        {
            InitializeComponent();
        }
        public Frm_FactoryAddr(int nFacId)
        {
            InitializeComponent();
            m_nSelectFacAddrTabID = nFacId;
        }
        private void Frm_FactoryAddr_Load(object sender, EventArgs e)
        {
            DataSet ds = m_bllFactoryDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxChooseFactory.DataSource = ds.Tables[0];
                this.comboBoxChooseFactory.DisplayMember = "工厂名称";
                this.comboBoxChooseFactory.ValueMember = "FactoryDataTabID";
                //this.comboBoxChooseFactory.SelectedIndex = 0;
                this.comboBoxChooseFactory.SelectedValue = m_nSelectFacAddrTabID;
            }
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDel.Visible = true;
            }
        }

        private void comboBoxChooseFactory_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string strCompID = this.comboBoxChooseFactory.SelectedValue.ToString();
                dataGridViewFactoryAddrList.DataSource = null;
                m_ds = m_bllFacAddrTab.GetAddrListByCompID(strCompID);
                if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
                {
                    dataGridViewFactoryAddrList.DataSource = m_ds.Tables[0];
                    dataGridViewFactoryAddrList.Columns[0].Visible = false;
                }
            }
            catch { }
        }

        private void ShowAllRecordInDataGridView()
        {
            dataGridViewFactoryAddrList.DataSource = null;
            m_ds = m_bllFacAddrTab.GetAllAddrList();
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewFactoryAddrList.DataSource = m_ds.Tables[0];
                dataGridViewFactoryAddrList.Columns[0].Visible = false;
            }
        }

        private void buttonShowAllRecord_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void dataGridViewFactoryAddrList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strFacAddrTabID = this.dataGridViewFactoryAddrList.Rows[this.dataGridViewFactoryAddrList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectFacAddrTabID = Convert.ToInt32(strFacAddrTabID, 10);
            cellclickFacAddrTab();
        }

        private void cellclickFacAddrTab()
        {
            DGBY.Model.FacAddrTab modelFacAddrTab = m_bllFacAddrTab.GetModel(m_nSelectFacAddrTabID);
            if (modelFacAddrTab != null)
            {
                if (modelFacAddrTab.FactoryDataTabID != null)
                    this.comboBoxChooseFactory.SelectedValue = modelFacAddrTab.FactoryDataTabID;
                this.textBoxFacAddr.Text = modelFacAddrTab.FacAddr;
            }
            else
            {
                this.textBoxFacAddr.Text = "";
            }
        }

        private void dataGridViewFactoryAddrList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewFactoryAddrList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            //if (Frm_Main.m_strLoginUser != "钟运甜" && Frm_Main.m_strLoginUser != "陈洁薇")
            if (!Frm_Main.m_lstPermitPerson.Contains(Frm_Main.m_strLoginUser))
            {
                MessageBox.Show("对不起，你没有权限修改工厂!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try
            {
                DGBY.Model.FacAddrTab modelFacAddrTab = new DGBY.Model.FacAddrTab();
                modelFacAddrTab.FacAddr = this.textBoxFacAddr.Text;

                if (this.comboBoxChooseFactory.SelectedValue != null)
                    modelFacAddrTab.FactoryDataTabID = Convert.ToInt32(this.comboBoxChooseFactory.SelectedValue.ToString(), 10);

                int nResult = m_bllFacAddrTab.Add(modelFacAddrTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加", this.comboBoxChooseFactory.Text + "(" + modelFacAddrTab.FacAddr + ")"); 
                    m_nSelectFacAddrTabID = m_bllFacAddrTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
       
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            if (!Frm_Main.m_lstPermitPerson.Contains(Frm_Main.m_strLoginUser))
            {
                MessageBox.Show("对不起，你没有权限修改工厂!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try
            {
                if (m_bllFacAddrTab.GetModel(m_nSelectFacAddrTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                DGBY.Model.FacAddrTab modelFacAddrTab = m_bllFacAddrTab.GetModel(m_nSelectFacAddrTabID);
                modelFacAddrTab.FacAddr = this.textBoxFacAddr.Text;
                //modelFacAddrTab.FacAddrTabID = m_nSelectFacAddrTabID;

                //if (this.comboBoxChooseFactory.SelectedValue != null)
                //    modelFacAddrTab.FactoryDataTabID = Convert.ToInt32(this.comboBoxChooseFactory.SelectedValue.ToString(), 10);

                m_bllFacAddrTab.Update(modelFacAddrTab);
                Frm_Main.Log2DB(this.Text, "修改", this.comboBoxChooseFactory.Text + "(" + modelFacAddrTab.FacAddr + ")"); 
                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (m_bllFacAddrTab.GetModel(m_nSelectFacAddrTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllFacAddrTab.Delete(m_nSelectFacAddrTabID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclickFacAddrTab();
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dataGridViewFactoryAddrList.DataSource;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "FacAddr.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.DataTableToExcel(dt, 30, 6, 3, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("数据不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}